<template>
	<el-form ref="form" :model="form" label-width="110px" @submit.prevent="onSubmit" style="margin:20px;width:60%;min-width:600px;">
		<el-form-item label="订单编号">
			<el-input v-model="form.orderId" :disabled="true"></el-input>
		</el-form-item>
		<!--<el-form-item label="商家shopuuid">-->
			<!--<el-input v-model="form.shopuuid"></el-input>-->
		<!--</el-form-item>-->
		<el-form-item label="分销商" prop="useruuid">
			<el-select v-model="form.shopuuid" placeholder="请选择分销商"
					   @change="userChange">
				<el-option value="0" label="全部">全部</el-option>
				<el-option
						v-for="item in users"
						:key="item.user.useruuid"
						:label="item.user.username"
						:value="item.user.useruuid">
				</el-option>
			</el-select>
		</el-form-item>

		<!--<el-form-item label="产品ID">-->
			<!--<el-input v-model="form.productuuid"></el-input>-->
		<!--</el-form-item>-->

		<el-form-item label="产品名称" prop="productuuid">
			<el-select v-model="form.productuuid" placeholder="请选择产品" @change="productChange">
				<el-option value="0" label="全部">全部</el-option>
				<el-option
						v-for="item in products"
						:key="item.downprouuid"
						:label="item.productName"
						:value="item.downprouuid">
				</el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="充值金额">
			<el-input v-model="form.amount" :disabled="true"></el-input>
		</el-form-item>

		<el-form-item label="充值号码">
			<el-input  type="textarea" v-model="form.phoneNum"  placeholder="批量充值时格式为18011112222,18900002222,15922223333"  ></el-input>
		</el-form-item>
		<el-form-item label="回调地址">
			<el-input v-model="form.notifyAddress"></el-input>
		</el-form-item>
		<el-form-item label="时间戳">
			<el-input v-model="form.ts"></el-input>
		</el-form-item>
		<!--<el-form-item label="key">-->
			<!--<el-input v-model="form.key"></el-input>-->
		<!--</el-form-item>-->
		<el-form-item label="SIGN">
			<el-input v-model="form.sign"></el-input>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" v-on:click="signMethod">签名</el-button>
			<el-button type="primary" v-on:click="onSubmit">提交充值</el-button>
			<el-button type="primary" v-on:click="onrestBatch">批量充值</el-button>

		</el-form-item>
	</el-form>
</template>

<script>
	import {
		testProduct,
		findAllDownShopInfo,
		findAllDownProduct,
		findDownShopInfoKey,
		restBatch,
		getConfig
	} from '../../api/api';
    var crypto = require('crypto');


	export default {
		data() {
//            {"orderId":"20180307163332772sssss","shopuuid":"00000000610a23d801610a333dd70006","amount":"30","sign":"31f669fcfccee8a80e338785a93ebc87","productuuid":"00000000624e620b0162529c29ab1059",
//                "phoneNum":"18825284468","notifyAddress":"http://192.168.3.1:8831/zzz","ts":"20180307163332"}
			return {
				form: {
					orderId: 'yidongpingtai'+new Date().getTime(),
					uid: '0',
					amount: '',
					pid: '',
					phone: '',
					notify: '',
					key: '',
					sign:'',
					ts:new Date().getTime()
				},
                users:[],
                products:[]
			}
		},
		methods: {
			onSubmit() {
			    if(this.form.shopuuid != "0" ){
			        if( this.form.orderId != ""){
			            if(this.form.productuuid != "0" && this.form.productuuid != ""){
			                if(this.form.key != ''){
                                let para = Object.assign({}, this.form);
                                para.amount = para.amount+"";
                                para.ts = para.ts+"";
                                testProduct(para).then( datas => {
                                    let { message, code, data } = datas;
                                    if (code !== "0") {
                                        this.$message({
                                            message: message,
                                            type: 'error'
                                        });
                                    }else{
                                        this.$message({
                                            message: "提交成功",
                                            type: 'info'
                                        });
                                    }
                                    this.form.orderId = 'yidongpingtai'+new Date().getTime();
                                    this.form.ts = new Date().getTime();
                                });
							}else{
                                this.$message({
                                    message: "请刷新后再尝试",
                                    type: 'error'
                                });
							}
						}else{
                            this.$message({
                                message: "请选择充值测试产品",
                                type: 'error'
                            });
						}
					}else{
                        this.$message({
                            message: "订单编号不能为空",
                            type: 'error'
                        });
					}
				}else{
                    this.$message({
                        message: "请选择充值测试渠道",
                        type: 'error'
                    });
				}

			},onrestBatch:function(){
				var orders = [];
				if(this.form.phoneNum != ''){
					this.form.phoneNum = this.form.phoneNum.indexOf("，") != -1  ? this.form.phoneNum.replace('，',',') : this.form.phoneNum;
					var phone = this.form.phoneNum.split(",");
					for(var i = 0 ; i < phone.length; i++){

						var order = new Object();
						order.orderId = Math.random().toString(36).substr(2)+new Date().getTime();
						order.shopuuid = this.form.shopuuid;
						order.amount = this.form.amount;
						order.phoneNum = phone[i];
						order.productuuid = this.form.productuuid;
						order.notifyAddress = this.form.notifyAddress;
						order.ts = this.form.ts;
						var md5 = crypto.createHash('md5');
						var string = "orderId="+order.orderId +"&shopuuid="+order.shopuuid+"&amount="+order.amount+"&phoneNum="+order.phoneNum+"&productuuid="+order.productuuid+"&notifyAddress="+order.notifyAddress+"&ts="+order.ts+"&key="+this.form.key
						order.sign = md5.update(string).digest('hex');
						orders.push(order)
					}
				}
				restBatch(JSON.stringify(orders)).then( datas => {
					let { message, code, data } = datas;
					if (code !== "0") {
						this.$message({
							message: message,
							type: 'error'
						});
					}else{
						this.$message({
							message: "提交成功",
							type: 'info'
						});
					}
					this.form.orderId = 'weiliupingtai'+new Date().getTime();
					this.form.ts = new Date().getTime();
				});

			},findAllDownShopInfo: function () {
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                findAllDownShopInfo(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users = data;
                    }
                });
            },
			userChange: function (shopuuid) {
				this.form.shopuuid = shopuuid;
                let para = {useruuid: this.form.shopuuid};
                findAllDownProduct(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.products = data;
                    }
                });
                findDownShopInfoKey(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                       this.form.key = data;
                    }
                });
			},productChange:function(productuuid){
				for(var i = 0 ; i < this.products.length ; i++){
				    if(this.products[i].downprouuid == productuuid){
                        this.form.amount = this.products[i].price;
					}
				}
			},
			signMethod(){
                var md5 = crypto.createHash('md5');
                var string = "orderId="+this.form.orderId+"&shopuuid="+this.form.shopuuid+"&amount="+this.form.amount+"&phoneNum="+this.form.phoneNum+"&productuuid="+this.form.productuuid+"&notifyAddress="+this.form.notifyAddress+"&ts="+this.form.ts+"&key="+this.form.key
				this.form.sign =  md5.update(string).digest('hex');
			},
			getConfig() {
				let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
				getConfig(para).then(datas => {
					let {message, code, data} = datas;
					if (code !== "0") {
						this.$message({
							message: message,
							type: 'error'
						});
					} else {
						this.form.orderId = data.testorderprefix + new Date().getTime();
						this.form.notify = data.testcallbackurl;
					}
				});
			}
		},mounted() {
            this.findAllDownShopInfo();
			this.getConfig();
        }
	}

</script>
